#include<iostream>
#include<unordered_map>
using namespace std;
const int N = 2e5 + 10;
unordered_map<int, int>S;
int n, m;
struct node
{
	int a, b, e;
}query[N];
int p[N];
int get(int x)
{
	if (S.count(x) == 0)S[x] = ++n;
	return S[x];
}
int find(int x)
{
	if (x != p[x]) {
		p[x] = find(p[x]);
	}
	return p[x];
}
int main()
{
	int t;
	cin >> t;
	while (t--) {
		S.clear();
		n = 0;
		cin >> m;
		for (int i = 0; i < m; i++) {
			int a, b, e;
			scanf("%d%d%d", &a, &b, &e);
			query[i] = { get(a),get(b),e };
		}
		for (int i = 1; i <= n; i++) {
			p[i] = i;
		}
		for (int i = 0; i < m; i++) {
			if (query[i].e == 1) {
				int a = find(query[i].a);
				int b = find(query[i].b);
				p[a] = b;
			}
		}
		bool flag = 0;
		for (int i = 0; i < m; i++) {
			if (query[i].e == 0) {
				int a = find(query[i].a);
				int b = find(query[i].b);
				if (a == b) {
					flag = 1;
					break;
				}
			}
		}
		if (flag)puts("NO");
		else puts("YES");
	}
	return 0;
}